//
//  ArticleViewController.m
//  Lmm_IOS
//
//  Created by 张哲炜 on 2018/10/9.
//  Copyright © 2018 lmm. All rights reserved.
//

#import "ArticleViewController.h"

@interface ArticleViewController ()

@end

@implementation ArticleViewController{
    UIWebView *webView;
    UILabel *titleLabel;
    UIView *navView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self getArticleDetail];
    
    [self drawView];
    
}



- (void) getArticleDetail {
    
    
   
    NSMutableDictionary *params = NSMutableDictionary.new;
    [params setValue:[NSString stringWithFormat:@"%d",self.id] forKey:@"id"];
    
    [HttpHelper request:@"article/detail" :@"GET" :params successBlock:^(NSDictionary *responseDic) {
        
       ArticleModel *articleModel = [ArticleModel mj_objectWithKeyValues:responseDic[@"data"]];
        
        self->titleLabel.text =articleModel.title;
        
        NSString *path = [[NSBundle mainBundle] bundlePath];
        NSURL *baseURL = [NSURL fileURLWithPath:path];
        
        [self->webView loadHTMLString:articleModel.content baseURL:baseURL];
        
    } failureBlock:^(NSError *error) {
        NSLog(@"请求失败");
    }];
    
}


- (void) drawView {
    
    
    self->navView = UIView.new;
    self->navView.backgroundColor = [UIColor whiteColor];
    
    int height = NAVIGATION_BAR_HEIGHT;
    int titleLabelOffset = 20;
    if (DEVICE_IS_IPHONEX) {
        height = NAVIGATION_BAR_X_HEIGHT;
        titleLabelOffset = 40;
    }
    
    [self.view addSubview:navView];
    
    [navView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.top).offset(0);
        make.left.equalTo(self.view.left).offset(0);
        make.right.equalTo(self.view.right).offset(0);
        make.height.equalTo(height);
    }];
    
    
    UIView *divideLine = UIView.new;
    divideLine.backgroundColor =UIColorFromRGB(0xCCCCCC) ;
    [navView addSubview:divideLine];
    
    [divideLine makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->navView.bottom);
        make.left.equalTo(self.view.left).offset(0);
        make.right.equalTo(self.view.right).offset(0);
        make.height.equalTo(1/[[UIScreen mainScreen] scale]);
    }];
    
    
    UIImageView *backImage = UIImageView.new;
    backImage.image = [UIImage imageNamed:@"back"];
    [navView addSubview:backImage];
    
    [backImage makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->navView.left).offset(15);
        make.top.equalTo(self->navView.top).offset(height-30);
        make.width.equalTo(10);
        make.height.equalTo(18);
    }];
    
    
    
    UIButton *backbtn = UIButton.new;
    [backbtn addTarget:self action:@selector(btnbackClick) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:backbtn];
    
    [backbtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->navView.left).offset(10);
        make.top.equalTo(self->navView.top).offset(20);
        make.width.equalTo(50);
        make.height.equalTo(40);
    }];
    
    
    self->titleLabel = UILabel.new;
    //self->titleLabe.text
    self->titleLabel.textColor =[UIColor grayColor];
    self->titleLabel.textAlignment = NSTextAlignmentCenter;
    self->titleLabel.font =[UIFont systemFontOfSize:16];
    [navView addSubview: self->titleLabel];

    [ self->titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(44);
        make.top.equalTo(self.view.top).offset(titleLabelOffset);
        make.width.equalTo(MAIN_SCREEN_WIDTH);
    }];
    
    
    
    
    self->webView = UIWebView.new;
    self->webView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self->webView];

//        // 解决webview页面底部出现一个黑色区域
////        if (@available(iOS 11.0, *)) {
////            self->webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
////        }
//
//
[self->webView makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self->navView.bottom).offset(0);
    make.left.equalTo(self.view.left).offset(0);
    make.right.equalTo(self.view.right).offset(0);
    make.bottom.equalTo(self.view.bottom).offset(0);
}];


}



-(void) btnbackClick {

       [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
